package com.shaozg.studydemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import com.shaozg.studydemo.view.MainPage1View;
import com.shaozg.studydemo.view.MainPage2View;

public class MainActivity extends Activity {

	private ViewPager mViewPager;
	
	private List<View> mViewList;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        
        initViews();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void initViews() {    	
    	mViewPager = (ViewPager)findViewById(R.id.main_viewpager);

    	mViewList = new ArrayList<View>(2);
    	mViewList.add(new MainPage1View(this).getLayoutView());
    	mViewList.add(new MainPage2View(this).getLayoutView());
    	
    	mViewPager.setAdapter(new MyPagerAdapter(mViewList));
    }
    
    class MyPagerAdapter extends PagerAdapter {
    	private List<View> listViews;

		public MyPagerAdapter(List<View> listViews) {
			super();
			this.listViews = listViews;
		}
    	
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return listViews.size();
    	}
    	
    	@Override
    	public boolean isViewFromObject(View arg0, Object arg1) {
    		// TODO Auto-generated method stub
    		return arg0 == arg1;
    	}
    	
    	@Override
    	public void destroyItem(View container, int position, Object object) {
    		// TODO Auto-generated method stub
    		((ViewPager)container).removeView(listViews.get(position));
    	}
    	
    	@Override
    	public Object instantiateItem(ViewGroup container, int position) {
    		// TODO Auto-generated method stub
    		((ViewPager)container).addView(listViews.get(position), 0);
    		return listViews.get(position);
    	}
    }
    
}
